package com.songlin.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import static android.R.attr.path;

/*
 *  图形处理工具
 *  处理圆角
 *  根据http获取图像
 */

public class BitmapUtils {

	//将Bitmap处理成圆形
	public static Bitmap toRoundBitmap(Bitmap bitmap){
		int width=bitmap.getWidth();
		int height=bitmap.getHeight();
		int r=0;
		//取最短边做边长
		if(width<height){
			r=width;
		}else{
			r=height;
		}
		//构建一个bitmap
		Bitmap backgroundBm=Bitmap.createBitmap(width,height,Config.ARGB_8888);
		//new一个Canvas，在backgroundBmp上画图 
		Canvas canvas=new Canvas(backgroundBm);
		Paint p=new Paint();
		//设置边缘光滑，去掉锯齿 
		p.setAntiAlias(true);
		RectF rect=new RectF(0, 0, r, r);
		//通过制定的rect画一个圆角矩形，当圆角X轴方向的半径等于Y轴方向的半径时，  
        //且都等于r/2时，画出来的圆角矩形就是圆形 
		canvas.drawRoundRect(rect, r/2, r/2, p);
		//设置当两个图形相交时的模式，SRC_IN为取SRC图形相交的部分，多余的将被去掉
		p.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		//canvas将bitmap画在backgroundBmp上
		canvas.drawBitmap(bitmap, null, rect, p);
		
		return backgroundBm;
	}
	//利用URL请求服务器头像，返回Bitmap
	public static Bitmap getHttpBitmap(String url){
		Bitmap bitmap=null;
		URL myUrl;
		
		try {
			myUrl=new URL(url);
			HttpURLConnection conn=(HttpURLConnection)myUrl.openConnection();
			conn.setConnectTimeout(5000);
			conn.connect();
			//打开输出流
			InputStream is=conn.getInputStream();
			bitmap=BitmapFactory.decodeStream(is);
			//把bitmap转成圆形
			bitmap=toRoundBitmap(bitmap);
			//关闭
			is.close();
		} catch (MalformedURLException e) {
			
			e.printStackTrace();
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		//返回圆形bitmap
		return bitmap;
	}

	public static void saveImageToGallery(Context context, Bitmap bmp, String imgName) {
		// 首先保存图片
		File appDir = new File(Environment.getExternalStorageDirectory(), "Bake");
		if (!appDir.exists()) {
			appDir.mkdir();
		}
		String fileName = imgName + ".jpg";
		File file = new File(appDir, fileName);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 其次把文件插入到系统图库
		try {
			MediaStore.Images.Media.insertImage(context.getContentResolver(),
					file.getAbsolutePath(), fileName, null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		// 最后通知图库更新
		context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
	}

	public static Bitmap getImage(String imgName) {
		// 首先保存图片

		File appDir = new File(Environment.getExternalStorageDirectory(), "Bake");
		String fileName = imgName + ".jpg";
		File file = new File(appDir, fileName);
		Bitmap bmp = BitmapFactory.decodeFile(file.toString());
		return bmp;
	}


}
